﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Media.Imaging;

namespace WGM_Silverlight.Views
{
    public partial class OptionsView : UserControl
    {
        MainView _parent;
        WGM_Svc.UserOptions _userOptions;
        ImageSource _initImageSource;
        byte[] _tempImageArr;
        bool _pendingImageChange = false;
        public OptionsView()
        {
            InitializeComponent();
            
        }
        public void Init(WGM_Svc.UserOptions uo, MainView parent, List<string> fontList, ImageSource userImage)
        {
            _parent = parent;
            
            _onlineOptionsFontChooser.ItemsSource = fontList;
            _onlineOptionsFontChooser.SelectedIndex = 0;
            _initImageSource = userImage;
            _userImage.ImageSource = userImage;
            SetUserOptions(uo);
            
        }
        private void SetUserOptions(WGM_Svc.UserOptions uo)
        {
            _userOptions = uo;
            _onlineOptionsDisplayNameTB.Text = uo.DisplayName;
            _onlineOptionsDisplayStatusTB.Text = uo.UserStatus;
            _onlineOptionsFontChooser.SelectedItem = uo.UserFontFace;
            _onlineOptionsFontColourR.Value = uo.FontColourR;
            _onlineOptionsFontColourG.Value = uo.FontColourG;
            _onlineOptionsFontColourB.Value = uo.FontColourB;
            _onlineOptionsFontSize.Value = uo.FontSize;
            _onlineOptionsIsBold.IsChecked = uo.IsFontBold ? true : false;
            _onlineOptionsIsItalics.IsChecked = uo.IsFontItalics ? true : false;
            _onlineOptionsIsUnderlined.IsChecked = uo.IsFontUnderlined ? true : false;

        }
        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            UpdateOnlineOptions();
        }

        private void UpdateOnlineOptions()
        {
            try
            {
                _onlineOptionsFontChooser.Foreground = new SolidColorBrush(Color.FromArgb(255,
                                                        (byte)_onlineOptionsFontColourR.Value,
                                                     (byte)_onlineOptionsFontColourG.Value,
                                                     (byte)_onlineOptionsFontColourB.Value));
                _onlineOptionsFontChooser.FontSize = _onlineOptionsFontSize.Value;
                if (_onlineOptionsIsBold.IsChecked == true)
                    _onlineOptionsFontChooser.FontWeight = FontWeights.Bold;
                else
                    _onlineOptionsFontChooser.FontWeight = FontWeights.Normal;
                if (_onlineOptionsIsItalics.IsChecked == true)
                    _onlineOptionsFontChooser.FontStyle = FontStyles.Italic;
                else
                    _onlineOptionsFontChooser.FontStyle = FontStyles.Normal;
            }
            catch { }
        }

        private void CheckBox_Changed(object sender, RoutedEventArgs e)
        {
            UpdateOnlineOptions();
        }

        private void DisplayImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "JPEG Files (*.jpeg)|*.jpeg|JPG Files (*.jpg)|*.jpg";
            ofd.Multiselect = false;
            if ((bool)ofd.ShowDialog())
            {
                using (Stream fs = ofd.File.OpenRead())
                {
                    BitmapImage image = new BitmapImage();
                    fs.Seek(0, SeekOrigin.Begin);
                    image.SetSource(fs);
                    _userImage.ImageSource = image;
                    _tempImageArr = new byte[fs.Length];
                    fs.Seek(0, SeekOrigin.Begin);
                    fs.Read(_tempImageArr, 0,(int) fs.Length);
                    _pendingImageChange = true;
                }
            }
        }

        private void ClearImgBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            _tempImageArr = null;
            _pendingImageChange = true;
            _userImage.ImageSource = new BitmapImage(new Uri("../Images/blank.png", UriKind.RelativeOrAbsolute));
        }

        private void ResetBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            SetUserOptions(_userOptions);
            _userImage.ImageSource = _initImageSource;
            _pendingImageChange = false;
        }

        private void DoneBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            WGM_Svc.UserOptions myNewOptions = new WGM_Svc.UserOptions();
            myNewOptions.FontColourR = (int)_onlineOptionsFontColourR.Value;
            myNewOptions.FontColourG = (int)_onlineOptionsFontColourG.Value;
            myNewOptions.FontColourB = (int)_onlineOptionsFontColourB.Value;
            
            myNewOptions.UserStatus = _onlineOptionsDisplayStatusTB.Text;
            myNewOptions.DisplayName = _onlineOptionsDisplayNameTB.Text;
            if (myNewOptions.DisplayName.Trim() == "")
                myNewOptions.DisplayName = myNewOptions.UserName;
            myNewOptions.IsFontBold = _onlineOptionsIsBold.IsChecked == true;
            myNewOptions.IsFontItalics = _onlineOptionsIsItalics.IsChecked == true;
            myNewOptions.IsFontUnderlined = _onlineOptionsIsUnderlined.IsChecked == true;
            myNewOptions.UserFontFace = _onlineOptionsFontChooser.SelectionBoxItem.ToString();
            myNewOptions.FontSize = (int)_onlineOptionsFontSize.Value;
            _parent.SetUserOptions(myNewOptions);
            if (_pendingImageChange)
                _parent.SetUserImage(_tempImageArr);
        }

        private void CancelBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            _parent.CancelChangeOptions();
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            UpdateOnlineOptions();
        }
    }
}
